import os
import subprocess
from pathlib import Path

def generate_schemas():
    # 获取项目根目录
    root_dir = Path(__file__).parent.parent.parent
    types_dir = root_dir / "shared" / "types"
    output_dir = root_dir / "api" / "schemas" / "generated"
    
    # 确保输出目录存在
    output_dir.mkdir(parents=True, exist_ok=True)
    
    # 遍历 types 目录中的所有 json 文件
    for schema_file in types_dir.glob("*.json"):
        output_file = output_dir / f"{schema_file.stem}.py"
        
        # 构建命令
        cmd = [
            "datamodel-codegen",
            "--input", str(schema_file),
            "--output", str(output_file),
            "--input-file-type", "jsonschema",
            "--output-model-type", "pydantic_v2.BaseModel"
        ]
        
        print(f"Generating {output_file} from {schema_file}")
        subprocess.run(cmd, check=True)

if __name__ == "__main__":
    generate_schemas() 